package com.yq.cp.service.yw.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.Position;
import com.yq.cp.entity.permission.User;
import com.yq.cp.entity.yw.YwGdxx;
import com.yq.cp.entity.yw.YwGdxxNzjl;
import com.yq.cp.mapper.basics.PositionMapper;
import com.yq.cp.mapper.permission.UserMapper;
import com.yq.cp.mapper.yw.YwGdxxMapper;
import com.yq.cp.mapper.yw.YwGdxxNzjlMapper;
import com.yq.cp.service.yw.YwGdxxNzjlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.utils.DateTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 运维-工单-扭转记录 服务实现类
 * </p>
 *
 * @author zh
 * @since 2024-09-07
 */
@Service
public class YwGdxxNzjlServiceImpl extends ServiceImpl<YwGdxxNzjlMapper, YwGdxxNzjl> implements YwGdxxNzjlService {

    @Resource
    YwGdxxNzjlMapper ywGdxxNzjlMapper;
    @Resource
    YwGdxxMapper ywGdxxMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    PositionMapper positionMapper;

    @Override
    public void nz(YwGdxxNzjl ywGdxxNzjl) {
        YwGdxx ywGdxx = ywGdxxMapper.selectById(ywGdxxNzjl.getGdxxId());
        ywGdxx.setNzzt(1);
        ywGdxx.setSlrId(ywGdxxNzjl.getNzgId());
        ywGdxxMapper.updateById(ywGdxx);
        Long nzgId = ywGdxxNzjl.getNzgId();


        ywGdxxNzjl.setNzrId(UserThreadLocal.get().getId());
        ywGdxxNzjl.setNzrName(UserThreadLocal.get().getNickname());
        ywGdxxNzjl.setNzsjTime(DateTimeUtil.dqsj());
        ywGdxxNzjl.setAuditState(1);

        User user1 = userMapper.selectById(ywGdxxNzjl.getNzrId());
        Position position = positionMapper.selectById(user1.getPositionId());
        if (position==null){
            ywGdxxNzjl.setCszw("站长");
        }else {
            ywGdxxNzjl.setCszw("负责人");
        }
        ywGdxxNzjlMapper.insert(ywGdxxNzjl);

        User user = userMapper.selectById(ywGdxxNzjl.getNzgId());
        YwGdxxNzjl ywGdxxNzjl2=new YwGdxxNzjl();
        ywGdxxNzjl2.setNzgId(ywGdxxNzjl.getNzgId());
        ywGdxxNzjl2.setNzrId(user.getId());
        ywGdxxNzjl2.setNzrName(user.getNickname());
        ywGdxxNzjl2.setAuditState(0);
        ywGdxxNzjl2.setGdxxId(ywGdxxNzjl.getGdxxId());

        User user2 = userMapper.selectById(ywGdxxNzjl.getNzgId());
        Position position2 = positionMapper.selectById(user2.getPositionId());
        if (position2==null){
            ywGdxxNzjl2.setCszw("站长");
        }else {
            ywGdxxNzjl2.setCszw("负责人");
        }
        ywGdxxNzjlMapper.insert(ywGdxxNzjl2);
    }

    @Override
    public void qxnz(YwGdxxNzjl ywGdxxNzjl) {
        YwGdxx ywGdxx = ywGdxxMapper.selectById(ywGdxxNzjl.getId());
        ywGdxx.setNzzt(0);
        ywGdxxMapper.updateById(ywGdxx);
//        ywGdxxNzjlMapper.sc(ywGdxxNzjl.getId());
        QueryWrapper<YwGdxxNzjl> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("gdxx_id",ywGdxx.getId());
        queryWrapper.orderByDesc("id");
        List<YwGdxxNzjl> ywGdxxNzjls = ywGdxxNzjlMapper.selectList(queryWrapper);
        for (int i = 0; i < ywGdxxNzjls.size(); i++) {
            if (i<2){
                Long id = ywGdxxNzjls.get(i).getId();
                ywGdxxNzjlMapper.sc1(id);
            }
        }
    }

    @Override
    public void jsnz(YwGdxxNzjl ywGdxxNzjl) {
        QueryWrapper<YwGdxxNzjl> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("gdxx_id",ywGdxxNzjl.getId());
        queryWrapper.orderByDesc("id");
        List<YwGdxxNzjl> ywGdxxNzjls = ywGdxxNzjlMapper.selectList(queryWrapper);
        if (ywGdxxNzjls==null && ywGdxxNzjls.size()==0){
            return;
        }
        YwGdxxNzjl nzjl = ywGdxxNzjls.get(0);

        if (nzjl!=null){
            nzjl.setAuditState(ywGdxxNzjl.getAuditState());
            nzjl.setAuditTime(DateTimeUtil.dqsj());
            nzjl.setAuditExcuse(ywGdxxNzjl.getAuditExcuse());
            nzjl.setAuditUserId(UserThreadLocal.get().getId());
            ywGdxxNzjlMapper.updateById(nzjl);

//            YwGdxx ywGdxx = ywGdxxMapper.selectById(nzjl.getGdxxId());
//            ywGdxx.setNzzt(0);
//            ywGdxx.setNzrId(null);
//            ywGdxxMapper.updateById(ywGdxx);
            ywGdxxMapper.xgxx(nzjl.getGdxxId(),nzjl.getNzrId());
        }
    }

    @Override
    public List<YwGdxxNzjl> all(YwGdxxNzjl ywGdxxNzjl) {
        QueryWrapper<YwGdxxNzjl> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("gdxx_id",ywGdxxNzjl.getId());
        queryWrapper.orderByAsc("id");
        List<YwGdxxNzjl> ywGdxxNzjls = ywGdxxNzjlMapper.selectList(queryWrapper);
        return ywGdxxNzjls;
    }
}
